package com.ilop.permission;

import android.content.DialogInterface;
import android.content.Intent;
import android.provider.Settings;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import com.ilop.permission.base.BottomTileDialog;
import com.ilop.permission.callback.OnDialogCallBack;
import com.ilop.permission.databinding.DialogAdvisedPermissionBinding;

/**
 * 建议开启定位权限
 * @author skygge@yeah.net
 * @date 6/20/22.
 */
public class AdvisedPermissionDialog extends BottomTileDialog<DialogAdvisedPermissionBinding> implements PermissionUtil.PermissionsCallBack{

    private OnDialogCallBack mCallBack;

    private FragmentActivity mActivity;

    public AdvisedPermissionDialog() {
    }

    public AdvisedPermissionDialog(FragmentActivity activity, OnDialogCallBack mCallBack) {
        this.mActivity = activity;
        this.mCallBack = mCallBack;
    }

    @Override
    protected int initLayoutId() {
        return R.layout.dialog_advised_permission;
    }

    @Override
    protected void initView() {
        String permission = getString(R.string.permission_location);
        mDBind.dialogAdvisedTitle.setText(getString(R.string.advised_permission, permission));
    }

    @Override
    protected void initData() {
        onRefreshAllow();
        onRefreshEnabled();
    }

    @Override
    protected void initListener() {
        mDBind.dialogAdvisedClose.setOnClickListener(view -> dismiss());
    }

    @Override
    protected void getResultLauncher(Intent intent) {
        onRefreshEnabled();
    }

    @Override
    public void onRequestAllGranted() {
        onRefreshAllow();
    }

    private void onRefreshAllow() {
        boolean allow = PermissionUtil.onGetLocationPermission(mContext);
        if (allow) {
            mDBind.dialogAdvisedState1.setText(getString(R.string.enabled));
            mDBind.dialogAdvisedState1.setTextColor(ContextCompat.getColor(mContext, R.color.text_sub_title));
            mDBind.dialogAdvisedState1.setOnClickListener(null);
        }
        else {
            mDBind.dialogAdvisedState1.setText(getString(R.string.set_up));
            mDBind.dialogAdvisedState1.setTextColor(ContextCompat.getColor(mContext, R.color.main_color));
            mDBind.dialogAdvisedState1.setOnClickListener(view -> PermissionUtil.onSetLocationPermissions(mActivity, this));
        }
    }

    private void onRefreshEnabled() {
        boolean enable = PermissionUtil.onGetLocationEnabled(mContext);
        if (enable) {
            mDBind.dialogAdvisedState2.setText(getString(R.string.enabled));
            mDBind.dialogAdvisedState2.setTextColor(ContextCompat.getColor(mContext, R.color.text_sub_title));
            mDBind.dialogAdvisedState2.setOnClickListener(null);
        }
        else {
            mDBind.dialogAdvisedState2.setText(getString(R.string.turn_on));
            mDBind.dialogAdvisedState2.setTextColor(ContextCompat.getColor(mContext, R.color.main_color));
            mDBind.dialogAdvisedState2.setOnClickListener(view -> mLauncher.launch(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)));
        }
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        if (mCallBack!=null) mCallBack.onConfirm();
        super.onDismiss(dialog);
    }
}
